package com.lyh.hkcloud.module.m1300029;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.lyh.hkcloud.R;
import com.lyh.hkcloud.base.BaseActivity;
import com.lyh.hkcloud.constants.BusinessConstants;
import com.lyh.hkcloud.constants.HttpUrlComstants;
import com.lyh.hkcloud.module.common.LinesEditView;
import com.lyh.hkcloud.module.m1300027.service.Service;
import com.lyh.hkcloud.util.base.JsonUtil;
import com.lyh.hkcloud.util.base.SaveData;
import com.lyh.hkcloud.util.base.ToastUtils;
import com.lyh.hkcloud.util.http.listener.json.JsonFailuredListener;
import com.lyh.hkcloud.util.http.listener.json.JsonSuccessListener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author: zhongfan
 * @program: hkCloud
 * @description: 盘点差异
 * @company—date: 利元数据 2022-02-18 17:07
 **/
public class IndexActivity extends BaseActivity implements View.OnClickListener{

    private ImageView goBack;
    private Button bt_save;
    private Service service;
    private LinesEditView lev_memo;

    String data;

    @Override
    protected Activity getActivity() {
        return this;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_1300029;
    }

    @Override
    protected void initView() {
        goBack = findViewById(R.id.go_back);
        goBack.setOnClickListener(this);
        bt_save=findViewById(R.id.bt_save);
        lev_memo=findViewById(R.id.lev_memo);
        getSupportActionBar().hide();

        data = getIntent().getStringExtra(BusinessConstants.DATA_ID);
        //查询备注
        this.queryNote(inMap(data));
    }

    @Override
    protected int getSubmitResId() {
        return R.id.bt_save;
    }

    @Override
    protected void onBeginInit() {
        service = new Service(IndexActivity.this);
    }

    @Override
    protected void onSubmit() {
        bt_save.setEnabled(false);
        JSONObject inMap=new JSONObject();
        try {
            //盘点单号
            inMap.put("dataid",new JSONObject(data).getString(BusinessConstants.FA_ID));
            inMap.put("memo",lev_memo.getContentText());
            inMap.put("userid", SaveData.getString(this,"userid"));
            inMap.put("username",  SaveData.getString(this,"userName"));

        } catch (JSONException e) {
            e.printStackTrace();
        }
        service.post(inMap, HttpUrlComstants.FA_MA_INV_DIFF_SAVE, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                onClick(goBack);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                ToastUtils.makeText(IndexActivity.this, msg, ToastUtils.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.go_back:
                Intent intent =new Intent(IndexActivity.this, com.lyh.hkcloud.module.m1300028.IndexActivity.class);
                intent.putExtra(BusinessConstants.DATA,data);
                IndexActivity.this.startActivity(intent);
                break;
        }
    }

    private JSONObject inMap(String dataStr){
        JSONObject inMap=new JSONObject();
        try {
            //盘点单号
            inMap.put(BusinessConstants.DATA_ID,new JSONObject(dataStr).getString(BusinessConstants.FA_ID));
            //inMap.put("memo",lev_memo.getContentText());
            inMap.put("userid", SaveData.getString(this,"userid"));
            inMap.put("username",  SaveData.getString(this,"userName"));

        }
        catch (Exception e){
            e.printStackTrace();
        }
        return inMap;
    }

    /**查询备注*/
    private void queryNote(JSONObject inMap) {
        service.post(inMap, HttpUrlComstants.GET_FA_MA_INV_DIF_MEMO, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                try {
                    JSONArray result = JsonUtil.getJsonObjectArray(response, "result", IndexActivity.this);
                    if (null != result && result.length() > 0) {
                        lev_memo.setContentText(result.getJSONObject(0).getString("difnote"));
                    }
                }catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                ToastUtils.makeText(IndexActivity.this, msg, ToastUtils.LENGTH_SHORT).show();
            }
        });
    }
}
